@isTest
private class ContractSendProductControllerTest {
    static testMethod void testMethod1() {
        ProductStructure__c ps1=new ProductStructure__c();
        ps1.Name='仪器';
        ps1.Classify1__c='生化';
        ps1.Classify2__c='生化-设备';
        ps1.Classify3__c='仪器';
        insert ps1;
        Brand__c brand = new Brand__c(
        	BrandShortName__c = '四川迈克'
        );
        insert brand;
       Province__c province  = new Province__c();
       province.Name = '四川';

       insert province ;

         City__c city = new City__c();
       city.Province__c = province.Id;
       insert city;


         Account acc = TestUtils.createAccount();
        acc.Level__c = '三级甲等';
        acc.RecordTypeId = Utils.getRecordTypeIdByDevNameAndObjName('Dealer', 'Account');
          acc.Province__c = province.Id;
        acc.City__c = city.Id;
        insert acc;     

        //  Dealer__c d = new Dealer__c(

        // 	Only__c='Dealer__c',
        // 	Name = acc.Name
        // );
        //  d.RelatedCustomers__c = acc.Id;
        //  insert d ;

        //业务机会1
        Opportunity opp = TestUtils.createOpportunity();
        opp.AccountId = acc.Id;
        opp.RecordTypeId = Utils.getRecordTypeIdByDevNameAndObjName('DealerOpportunity', 'Opportunity');
        insert opp;

        Id pRT = Utils.getRecordTypeIdByDevNameAndObjName('NCProduct', 'Product__c');

     	Product__c p  = TestUtils.createInst();
        p.Name = '测试仪器';
        p.RecordTypeId = pRT;
        p.Category0__c  = '仪器';
        p.Category1__c ='生化';
        p.ProductName__c = '测试仪器';
        p.ProductStructure__c = ps1.Id;
        p.CRMBrand__c = brand.Id;
        p.NCCode__c = '10';
        p.InstrumentType__c = '维保仪器';
        insert p ;



        Product__c p1  = TestUtils.createInst();
        p1.Name = '测试配件';
        p1.RecordTypeId = pRT;
        p1.Category0__c  = '配件';
        p1.ProductName__c = '测试配件';
        p1.NCCode__c = '01';
        insert p1 ;


         Product__c p2 = TestUtils.createInst();
        p2.Name = '配套软件';
        p2.RecordTypeId = pRT;
        p2.Category0__c  = '配件';
        p2.ProductName__c = '配套软件';
       	p2.MatchingInstrument__c = p.Id;
       	 p2.NCCode__c = '02';
        insert p2 ;


         Product__c p3= TestUtils.createInst();
        p3.Name = '打印机';
        p3.RecordTypeId = pRT;
        p3.ProductName__c = '打印机';
         p3.NCCode__c = '03';
       	//p3.MatchingInstrument__c = p.Id;
        insert p3 ;


        ConfigurationListDetail__c cfg = new ConfigurationListDetail__c();
        cfg.Amount__c = 1;
        cfg.Configuration__c  = p1.Id;
        cfg.InstrumentModel__c = p.Id;
        cfg.OrderMethod__c = '仪器配送';
        cfg.IsStandard__c = '必须配备（非标配）';
        cfg.Name = p1.Name;
        insert cfg;







        Contract__c con = new Contract__c();
        con.Account__c = acc.Id;
        con.Opportunity__c = opp.Id;
       	con.Status__c  = '已生效';
        con.Corpcode__c = 'MKSY';
        con.RecordTypeId = Utils.getRecordTypeIdByDevNameAndObjName('TerminalContract', 'Contract__c');
        insert con;


          //报价2
        Quote qt2 = new Quote(Name = '试剂报价',Type__c = '试剂报价',OpportunityId = opp.Id,IsActive__c = true);
        qt2.Status = '已批准';
        qt2.Contract__c = con.Id;
        insert qt2 ;

        QuoteProduct__c qp = new QuoteProduct__c(Name = '测试报价产品2',Product__c = p.Id,Quote__c = qt2.Id,SalesPrice__c = 5);
        insert qp ;


        ContractProduct__c cp = new ContractProduct__c();
        cp.Contract__c = con.Id;
        cp.Product__c =p.Id  ;
        cp.Quantity__c = 5;
        cp.OriginNumber__c = 5;
        cp.SalesPrice__c = 1; 
        cp.QuoteProduct__c = qp.Id;
        cp.CoopType__c = '正常销售';
        cp.ApplicationQuantity__c = 5;
        cp.Opportunity__c = opp.Id;
        insert cp ;


        ContractProduct__c cp1 = new ContractProduct__c();
        cp1.Contract__c = con.Id;
        cp1.Product__c =p2.Id  ;
        cp1.Quantity__c = 5;
        cp1.OriginNumber__c = 5;
        cp1.SalesPrice__c = 0; 
        cp1.CoopType__c = '正常销售';
        cp1.ApplicationQuantity__c = 5;
        cp1.Opportunity__c = opp.Id;
        insert cp1 ;


        Equipment__c e1 = new Equipment__c();
        e1.Account__c = acc.Id;
        e1.IMEINumber__c = '11';
        e1.Product__c = p.Id;
        insert e1;

        PageReference page = new PageReference('/apex/ContractSendProduct?id='+opp.Id);
        System.Test.setCurrentPage(page);
        ContractSendProductController scp = new ContractSendProductController();

        scp.init();
        scp.setNil();

        Map<String,String> installMap = new Map<String,String>();
        installMap.put('isReturn', 'true');
        installMap.put('cpId', cp.Id);
        installMap.put('returnEquipId', e1.Id );
        installMap.put('installAccId', acc.Id);

        scp.installJson = EncodingUtil.urlEncode(JSON.serialize(installMap), 'UTF-8');


        scp.createInstall();

          scp.editInstallApply();
           System.Test.startTest();
           
        Map<String,Object> saveMap = new Map<String,Object>();
        Map<String,String> accMap =new Map<String,String>();
        accMap.put('Province__c', province.Id);
        accMap.put('City__c', city.Id);

        saveMap.put('Account', accMap);

        Map<String,String> iaMap =  new Map<String,String>();
        InstallationApply__c ia ;
        iaMap.put('InstallationType__c', '同机身号仪器更换客户');
        iaMap.put('Hospital__c', acc.Id);
        saveMap.put('InstallationApply__c',iaMap );

        scp.saveJson = EncodingUtil.urlEncode(JSON.serialize(saveMap), 'UTF-8');

        scp.save();




        PageReference page1 = new PageReference('/apex/ContractSendProduct?id='+con.Id);
        System.Test.setCurrentPage(page1);
        ContractSendProductController scp1 = new ContractSendProductController();
        scp1.init();

        scp1.editInstallApply();
    }
}